<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>MC823 - Laboratório de Redes: Trabalho 01: Código-Fonte de beejServer.c</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body onload='searchBox.OnSelectItem(0);'>
<!-- Gerado por Doxygen 1.7.4 -->
<script type="text/javascript"><!--
var searchBox = new SearchBox("searchBox", "search",false,'Busca');
--></script>
<div id="top">
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td style="padding-left: 0.5em;">
   <div id="projectname">MC823 - Laboratório de Redes: Trabalho 01</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Página&#160;Principal</span></a></li>
      <li><a href="annotated.html"><span>Estruturas&#160;de&#160;Dados</span></a></li>
      <li class="current"><a href="files.html"><span>Arquivos</span></a></li>
      <li id="searchli">
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Busca" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>Lista&#160;de&#160;Arquivos</span></a></li>
      <li><a href="globals.html"><span>Globais</span></a></li>
    </ul>
  </div>
<div class="header">
  <div class="headertitle">
<div class="title">beejServer.c</div>  </div>
</div>
<div class="contents">
<a href="beejServer_8c.html">Vá para a documentação deste arquivo.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
<a name="l00002"></a>00002 <span class="comment">** server.c -- a stream socket server demo</span>
<a name="l00003"></a>00003 <span class="comment">*/</span>
<a name="l00004"></a>00004 
<a name="l00005"></a>00005 <span class="preprocessor">#include &lt;stdio.h&gt;</span>
<a name="l00006"></a>00006 <span class="preprocessor">#include &lt;stdlib.h&gt;</span>
<a name="l00007"></a>00007 <span class="preprocessor">#include &lt;unistd.h&gt;</span>
<a name="l00008"></a>00008 <span class="preprocessor">#include &lt;errno.h&gt;</span>
<a name="l00009"></a>00009 <span class="preprocessor">#include &lt;string.h&gt;</span>
<a name="l00010"></a>00010 <span class="preprocessor">#include &lt;sys/types.h&gt;</span>
<a name="l00011"></a>00011 <span class="preprocessor">#include &lt;sys/socket.h&gt;</span>
<a name="l00012"></a>00012 <span class="preprocessor">#include &lt;netinet/in.h&gt;</span>
<a name="l00013"></a>00013 <span class="preprocessor">#include &lt;netdb.h&gt;</span>
<a name="l00014"></a>00014 <span class="preprocessor">#include &lt;arpa/inet.h&gt;</span>
<a name="l00015"></a>00015 <span class="preprocessor">#include &lt;sys/wait.h&gt;</span>
<a name="l00016"></a>00016 <span class="preprocessor">#include &lt;signal.h&gt;</span>
<a name="l00017"></a>00017 
<a name="l00018"></a>00018 <span class="preprocessor">#include &quot;<a class="code" href="resposta_8h.html">resposta.h</a>&quot;</span>
<a name="l00019"></a>00019 <span class="preprocessor">#include &quot;<a class="code" href="tempo_8h.html">tempo.h</a>&quot;</span>
<a name="l00020"></a>00020 
<a name="l00021"></a><a class="code" href="beejServer_8c.html#a614217d263be1fb1a5f76e2ff7be19a2">00021</a> <span class="preprocessor">#define PORT &quot;39410&quot;  // the port users will be connecting to</span>
<a name="l00022"></a><a class="code" href="beejServer_8c.html#aeefbbafa97642defe3ee6c3080b7d66f">00022</a> <span class="preprocessor"></span><span class="preprocessor">#define BACKLOG 10   // how many pending connections queue will hold</span>
<a name="l00023"></a><a class="code" href="beejServer_8c.html#a16c16f9369be4a374a3e621f6d13bb16">00023</a> <span class="preprocessor"></span><span class="preprocessor">#define MAXDATASIZE 100</span>
<a name="l00024"></a>00024 <span class="preprocessor"></span>
<a name="l00025"></a>00025 
<a name="l00026"></a>00026 
<a name="l00027"></a>00027 
<a name="l00028"></a>00028 <span class="comment">/* --------------------------------------------------------------------------*/</span>
<a name="l00035"></a>00035 <span class="comment">/* ----------------------------------------------------------------------------*/</span>
<a name="l00036"></a><a class="code" href="beejServer_8c.html#a6c2d9589ac70568b8e3a6980bb3d45d6">00036</a> <span class="keywordtype">void</span> <a class="code" href="beejServer_8c.html#a6c2d9589ac70568b8e3a6980bb3d45d6">sigchld_handler</a>(<span class="keywordtype">int</span> s)
<a name="l00037"></a>00037 {
<a name="l00038"></a>00038     <span class="keywordflow">while</span>(waitpid(-1, NULL, WNOHANG) &gt; 0);
<a name="l00039"></a>00039 }
<a name="l00040"></a>00040 
<a name="l00041"></a>00041 <span class="comment">// get sockaddr, IPv4 or IPv6</span>
<a name="l00042"></a>00042 <span class="comment">/* --------------------------------------------------------------------------*/</span>
<a name="l00051"></a>00051 <span class="comment">/* ----------------------------------------------------------------------------*/</span>
<a name="l00052"></a><a class="code" href="beejServer_8c.html#a294867ba9d7ff47e39d421134d8e12ab">00052</a> <span class="keywordtype">void</span> *<a class="code" href="beejClient_8c.html#a294867ba9d7ff47e39d421134d8e12ab">get_in_addr</a>(<span class="keyword">struct</span> sockaddr *sa)
<a name="l00053"></a>00053 {
<a name="l00054"></a>00054     <span class="keywordflow">if</span> (sa-&gt;sa_family == AF_INET) {
<a name="l00055"></a>00055         <span class="keywordflow">return</span> &amp;(((<span class="keyword">struct </span>sockaddr_in*)sa)-&gt;sin_addr);
<a name="l00056"></a>00056     }
<a name="l00057"></a>00057 
<a name="l00058"></a>00058     <span class="keywordflow">return</span> &amp;(((<span class="keyword">struct </span>sockaddr_in6*)sa)-&gt;sin6_addr);
<a name="l00059"></a>00059 }
<a name="l00060"></a>00060 
<a name="l00061"></a>00061 <span class="comment">/* --------------------------------------------------------------------------*/</span>
<a name="l00071"></a>00071 <span class="comment">/* ----------------------------------------------------------------------------*/</span>
<a name="l00072"></a><a class="code" href="beejServer_8c.html#a840291bc02cba5474a4cb46a9b9566fe">00072</a> <span class="keywordtype">int</span> <a class="code" href="beejClient_8c.html#a0ddf1224851353fc92bfbff6f499fa97">main</a>(<span class="keywordtype">void</span>)
<a name="l00073"></a>00073 {
<a name="l00074"></a>00074     <span class="keywordtype">int</span> sockfd, new_fd;  <span class="comment">// listen on sock_fd, new connection on new_fd</span>
<a name="l00075"></a>00075     <span class="keyword">struct </span>addrinfo hints, *servinfo, *p;
<a name="l00076"></a>00076     <span class="keyword">struct </span>sockaddr_storage their_addr; <span class="comment">// connector&#39;s address information</span>
<a name="l00077"></a>00077     socklen_t sin_size;
<a name="l00078"></a>00078     <span class="keyword">struct </span>sigaction sa;
<a name="l00079"></a>00079     <span class="keywordtype">int</span> yes=1;
<a name="l00080"></a>00080     <span class="keywordtype">char</span> s[INET6_ADDRSTRLEN];
<a name="l00081"></a>00081     <span class="keywordtype">int</span> rv;
<a name="l00082"></a>00082 <span class="comment">/*Novidades******************************************/</span>
<a name="l00083"></a>00083     <span class="keywordtype">int</span> numbytes;
<a name="l00084"></a>00084     <span class="keywordtype">char</span> buf[<a class="code" href="beejServer_8c.html#a16c16f9369be4a374a3e621f6d13bb16">MAXDATASIZE</a>];
<a name="l00085"></a>00085     <span class="keywordtype">char</span> *resposta;
<a name="l00086"></a>00086 
<a name="l00087"></a>00087 
<a name="l00088"></a>00088     <span class="keyword">struct </span>timeval startTime; <span class="comment">//Estrutura contendo o tempo de início de execução</span>
<a name="l00089"></a>00089     <span class="keyword">struct </span>timeval endTime; <span class="comment">//Estrutura contendo o tempo final de execução</span>
<a name="l00090"></a>00090     <span class="keywordtype">double</span> time;
<a name="l00091"></a>00091     <span class="comment">/*******************************************/</span>   
<a name="l00092"></a>00092 
<a name="l00093"></a>00093     memset(&amp;hints, 0, <span class="keyword">sizeof</span> hints);
<a name="l00094"></a>00094     hints.ai_family = AF_UNSPEC;
<a name="l00095"></a>00095     hints.ai_socktype = SOCK_STREAM;
<a name="l00096"></a>00096     hints.ai_flags = AI_PASSIVE; <span class="comment">// use my IP</span>
<a name="l00097"></a>00097 
<a name="l00098"></a>00098     <span class="keywordflow">if</span> ((rv = getaddrinfo(NULL, <a class="code" href="beejServer_8c.html#a614217d263be1fb1a5f76e2ff7be19a2">PORT</a>, &amp;hints, &amp;servinfo)) != 0) {
<a name="l00099"></a>00099         fprintf(stderr, <span class="stringliteral">&quot;getaddrinfo: %s\n&quot;</span>, gai_strerror(rv));
<a name="l00100"></a>00100         <span class="keywordflow">return</span> 1;
<a name="l00101"></a>00101     }
<a name="l00102"></a>00102 
<a name="l00103"></a>00103     <span class="comment">// loop through all the results and bind to the first we can</span>
<a name="l00104"></a>00104     <span class="keywordflow">for</span>(p = servinfo; p != NULL; p = p-&gt;ai_next) {
<a name="l00105"></a>00105         <span class="keywordflow">if</span> ((sockfd = socket(p-&gt;ai_family, p-&gt;ai_socktype,
<a name="l00106"></a>00106                 p-&gt;ai_protocol)) == -1) {
<a name="l00107"></a>00107             perror(<span class="stringliteral">&quot;server: socket&quot;</span>);
<a name="l00108"></a>00108             <span class="keywordflow">continue</span>;
<a name="l00109"></a>00109         }
<a name="l00110"></a>00110 
<a name="l00111"></a>00111         <span class="keywordflow">if</span> (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &amp;yes,
<a name="l00112"></a>00112                 <span class="keyword">sizeof</span>(<span class="keywordtype">int</span>)) == -1) {
<a name="l00113"></a>00113             perror(<span class="stringliteral">&quot;setsockopt&quot;</span>);
<a name="l00114"></a>00114             exit(1);
<a name="l00115"></a>00115         }
<a name="l00116"></a>00116 
<a name="l00117"></a>00117         <span class="keywordflow">if</span> (bind(sockfd, p-&gt;ai_addr, p-&gt;ai_addrlen) == -1) {
<a name="l00118"></a>00118             close(sockfd);
<a name="l00119"></a>00119             perror(<span class="stringliteral">&quot;server: bind&quot;</span>);
<a name="l00120"></a>00120             <span class="keywordflow">continue</span>;
<a name="l00121"></a>00121         }
<a name="l00122"></a>00122 
<a name="l00123"></a>00123         <span class="keywordflow">break</span>;
<a name="l00124"></a>00124     }
<a name="l00125"></a>00125 
<a name="l00126"></a>00126     <span class="keywordflow">if</span> (p == NULL)  {
<a name="l00127"></a>00127         fprintf(stderr, <span class="stringliteral">&quot;server: failed to bind\n&quot;</span>);
<a name="l00128"></a>00128         <span class="keywordflow">return</span> 2;
<a name="l00129"></a>00129     }
<a name="l00130"></a>00130 
<a name="l00131"></a>00131     freeaddrinfo(servinfo); <span class="comment">// all done with this structure</span>
<a name="l00132"></a>00132 
<a name="l00133"></a>00133     <span class="keywordflow">if</span> (listen(sockfd, <a class="code" href="beejServer_8c.html#aeefbbafa97642defe3ee6c3080b7d66f">BACKLOG</a>) == -1) {
<a name="l00134"></a>00134         perror(<span class="stringliteral">&quot;listen&quot;</span>);
<a name="l00135"></a>00135         exit(1);
<a name="l00136"></a>00136     }
<a name="l00137"></a>00137 
<a name="l00138"></a>00138     sa.sa_handler = <a class="code" href="beejServer_8c.html#a6c2d9589ac70568b8e3a6980bb3d45d6">sigchld_handler</a>; <span class="comment">// reap all dead processes</span>
<a name="l00139"></a>00139     sigemptyset(&amp;sa.sa_mask);
<a name="l00140"></a>00140     sa.sa_flags = SA_RESTART;
<a name="l00141"></a>00141     <span class="keywordflow">if</span> (sigaction(SIGCHLD, &amp;sa, NULL) == -1) {
<a name="l00142"></a>00142         perror(<span class="stringliteral">&quot;sigaction&quot;</span>);
<a name="l00143"></a>00143         exit(1);
<a name="l00144"></a>00144     }
<a name="l00145"></a>00145 
<a name="l00146"></a>00146     printf(<span class="stringliteral">&quot;server: waiting for connections...\n&quot;</span>);
<a name="l00147"></a>00147 
<a name="l00148"></a>00148     <span class="keywordflow">while</span>(1) {  <span class="comment">// main accept() loop</span>
<a name="l00149"></a>00149         sin_size = <span class="keyword">sizeof</span> their_addr;
<a name="l00150"></a>00150         new_fd = accept(sockfd, (<span class="keyword">struct</span> sockaddr *)&amp;their_addr, &amp;sin_size);
<a name="l00151"></a>00151         <span class="keywordflow">if</span> (new_fd == -1) {
<a name="l00152"></a>00152             perror(<span class="stringliteral">&quot;accept&quot;</span>);
<a name="l00153"></a>00153             <span class="keywordflow">continue</span>;
<a name="l00154"></a>00154         }
<a name="l00155"></a>00155 
<a name="l00156"></a>00156         inet_ntop(their_addr.ss_family,
<a name="l00157"></a>00157             <a class="code" href="beejClient_8c.html#a294867ba9d7ff47e39d421134d8e12ab">get_in_addr</a>((<span class="keyword">struct</span> sockaddr *)&amp;their_addr),
<a name="l00158"></a>00158             s, <span class="keyword">sizeof</span> s);
<a name="l00159"></a>00159         printf(<span class="stringliteral">&quot;server: got connection from %s\n&quot;</span>, s);
<a name="l00160"></a>00160 
<a name="l00161"></a>00161         <span class="keywordflow">if</span> (!fork()) { <span class="comment">// this is the child process</span>
<a name="l00162"></a>00162             close(sockfd); <span class="comment">// child doesn&#39;t need the listener</span>
<a name="l00163"></a>00163 
<a name="l00164"></a>00164 <span class="comment">/*receiving request******************************************/</span>          
<a name="l00165"></a>00165             <span class="keywordflow">if</span> ((numbytes = recv(new_fd, buf, <a class="code" href="beejServer_8c.html#a16c16f9369be4a374a3e621f6d13bb16">MAXDATASIZE</a>-1, 0)) == -1) {
<a name="l00166"></a>00166                perror(<span class="stringliteral">&quot;recv&quot;</span>);
<a name="l00167"></a>00167                exit(1);
<a name="l00168"></a>00168             }
<a name="l00169"></a>00169             
<a name="l00170"></a>00170             <span class="comment">//Inicia a contagem de tempo, antes do write (recv)</span>
<a name="l00171"></a>00171             gettimeofday(&amp;startTime, NULL);
<a name="l00172"></a>00172 
<a name="l00173"></a>00173             buf[numbytes] = <span class="charliteral">&#39;\0&#39;</span>;
<a name="l00174"></a>00174             resposta = <a class="code" href="resposta_8c.html#a9bb7e31e6340d62b3afb839655847563">preparaResposta</a>(buf);
<a name="l00175"></a>00175             
<a name="l00176"></a>00176             <span class="comment">//Termina a contagem de tempo, após o read (send)</span>
<a name="l00177"></a>00177             gettimeofday(&amp;endTime, NULL);
<a name="l00178"></a>00178 
<a name="l00179"></a>00179             <span class="keywordflow">if</span> (send(new_fd, resposta, strlen(resposta), 0) == -1)
<a name="l00180"></a>00180                 perror(<span class="stringliteral">&quot;send&quot;</span>);
<a name="l00181"></a>00181             
<a name="l00182"></a>00182             time = <a class="code" href="tempo_8c.html#afa8cb739ea2c6f04b660789e2e36e9ee">calculaTempo</a>(startTime, endTime);
<a name="l00183"></a>00183             printf(<span class="stringliteral">&quot;Processing Time: %.2lf microseconds\n&quot;</span>, time);
<a name="l00184"></a>00184             
<a name="l00185"></a>00185             close(new_fd);
<a name="l00186"></a>00186             exit(0);
<a name="l00187"></a>00187         }
<a name="l00188"></a>00188        close(new_fd);  <span class="comment">// parent doesn&#39;t need this</span>
<a name="l00189"></a>00189     }
<a name="l00190"></a>00190     <span class="keywordflow">return</span> 0;
<a name="l00191"></a>00191 }
<a name="l00192"></a>00192 
</pre></div></div>
</div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>Todos</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Estruturas de Dados</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Arquivos</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Funções</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variáveis</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Definições de Tipos</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Definições e Macros</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<hr class="footer"/><address class="footer"><small>Gerado em Quarta, 25 de Abril de 2012 23:42:04 para MC823 - Laboratório de Redes: Trabalho 01 por&#160;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.4 </small></address>
</body>
</html>
